package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.BusinessProjectDetail;
import org.linlinjava.litemall.admin.dto.BusinessResultDetail;
import org.linlinjava.litemall.admin.service.AdBusinessResultService;
import org.linlinjava.litemall.admin.service.LogHelper;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.linlinjava.litemall.db.domain.BusinessResult;
import org.linlinjava.litemall.db.service.BusinessResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: Daisen.Z
 * @Date: 2021/6/6 12:04
 * @Version: 1.0
 * @Description: 查看项目结果的通用接口
 */
@RestController
@RequestMapping("/admin/result")
@Validated
public class AdBusinessResultController {

    private final Log logger = LogFactory.getLog(AdBusinessResultController.class);
    @Autowired
    private LogHelper logHelper;

    @Autowired
    private AdBusinessResultService adBusinessResultService;

    @RequiresPermissions("admin:result:list")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "查询"}, button = "查询")
    @GetMapping("/list")
    public Object list(String resYear,String resName,String resMark,String resWithProjectName,Integer owningModule,Integer statusAudit,String dutyPersonalName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return  adBusinessResultService.querySelective(resYear, resName,resMark, resWithProjectName,owningModule,statusAudit,dutyPersonalName,
                page, limit, sort, order);
    }

    /**
     * 查询项目相关成果
     * @param businessResultDetail
     * @return
     */
    @RequiresPermissions("admin:result:create")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "新增"}, button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody BusinessResultDetail businessResultDetail) {
        return adBusinessResultService.create(businessResultDetail);
    }

    /**
     * 删除科研项目
     *
     * @param businessResult
     * @return
     */
    @RequiresPermissions("admin:result:delete")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "删除"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody BusinessResult businessResult) {
        try {
            Object delete = adBusinessResultService.delete(businessResult);
            logHelper.logOrderSucceed("删除","删除项目成果:"+businessResult.getResName());
            return delete;
        } catch (Exception e) {
            logHelper.logOrderFail("删除","删除项目成果:"+businessResult.getResName());
            return ResponseUtil.serious();
        }
    }

    // 详情
    @RequiresPermissions("admin:result:detail")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "详情"}, button = "详情")
    @PostMapping("/detail")
    public Object detail( @RequestBody BusinessResult businessResult) {
        return adBusinessResultService.detail(businessResult.getResId());
    }


    // 更新：修改第一负责人要同步修改项目负责人，添加项目成员时第一负责人只能有一个
    @RequiresPermissions("admin:result:update")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "更新"}, button = "更新")
    @PostMapping("/update")
    public Object update(@RequestBody BusinessResultDetail businessResultDetail) {
        return adBusinessResultService.update(businessResultDetail);
    }


    // 查询成员贡献信息，modelId = -1代表查询全部,返回值中key是成员角色，valut是相关信息集合
    @RequiresPermissions("admin:result:listPerformance ")
    @RequiresPermissionsDesc(menu = {"项目成果管理", "查询绩效"}, button = "查询绩效")
    @GetMapping("/listPerformance")
    public Object listPerformance(Integer modelId,String personName) {
        return adBusinessResultService.listPerformance(modelId,personName);
    }

}
